package arithmetic.deleteduplicates;

/**
 * 给定一个已排序的链表的头 head ， 删除所有重复的元素，使每个元素只出现一次 。返回 已排序的链表 。
 */
public class Test {

    public static void main(String[] args) {

    }

    public static ListNode deleteDuplicates(ListNode head){
        if(head == null) return head;
        ListNode currentNode=head;
        while (null!= currentNode.next){
            if(currentNode.val == currentNode.next.val){
                currentNode.next=currentNode.next.next;
            }else {
                currentNode=currentNode.next;
            }
        }
        return head;
    }

    /**
     * 递归
     * @param head
     * @return
     */
    public static ListNode deleteDuplicates1(ListNode head){
        if (head == null || head.next == null) return head;
        head.next=deleteDuplicates(head.next);
        return head.val == head.next.val ? head.next : head;
    }

}

class ListNode {
    int val;
    ListNode next;
    ListNode() {}
    ListNode(int val) { this.val = val; }
    ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}
